<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
</head>
<body>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layuiadmin/layui/css/layui.css"
          media="all">
    <!--  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">-->
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style type="text/css">
        element.style {
            top: 0px;
        }
    </style>
</head>

<body>
<!-- 修改的弹窗 -->
<div id="editFormDiv" style="display: none">
    <form class="layui-form" lay-filter="example" method="post">
        <div class="layui-form-item" style="display: none">
            <label class="layui-form-label">用户编号</label>
            <div class="layui-input-block">
                <input type="text" name="userId" id="userid" lay-verify="title"
                       autocomplete="off" readonly class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="userName" lay-verify="title"
                       autocomplete="off" class="layui-input" id="username">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="男" title="男" checked="" id="nan">
                <input type="radio" name="sex" value="女" title="女" id="nv">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="tel" autocomplete="off"
                       class="layui-input" id="tel">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" lay-verify="title"
                       autocomplete="off" class="layui-input" id="email">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">出生年月</label>
            <div class="layui-input-block">
                <input type="text" name="birthday" id="date1" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status" lay-filter="aihao">
                    <option value="1">正常</option>
                    <option value="0">禁用</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn btn-update" type="button" lay-submit=""
                        lay-filter="demo1">立即提交
                </button>
            </div>
        </div>
    </form>
</div>


<div class="layui-form-item demoTable">
    <div class="layui-inline">
        <label class="layui-form-label">用户ID</label>
        <div class="layui-input-block">
            <input type="text" name="content" id="content" placeholder="请输入"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <button class="layui-btn layuiadmin-btn-useradmin" lay-submit
                lay-filter="LAY-user-front-search" data-type="reload">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
        </button>
    </div>

</div>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../layuiadmin/layui/layui.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
    layui.use(['table', 'laydate'], function () {
        var laydate = layui.laydate;
        var table = layui.table;
        table.render({
            elem: '#test'
            //  ,url:'/test/table/demo1.json'
            ,
            url: '../../userapi/user' //模拟接口
            ,
            toolbar: '#toolbarDemo',
            title: '用户数据表',
            cols: [[{
                type: 'checkbox',
                fixed: 'left'
            }, {
                field: 'userId',
                width: 100,
                title: '用户ID',
                sort: true
            }, {
                field: 'userName',
                title: '姓名',
                minWidth: 100
            }, {
                field: 'sex',
                width: 80,
                title: '性别'
            }, {
                field: 'tel',
                title: '手机'
            }, {
                field: 'email',
                title: '邮箱'
            }, {
                field: 'birthday',
                title: '生日'
            }, {
                field: 'status',
                title: '用户状态',
                templet: function (res) {
                    return res.status == 1 ? "正常" : "禁用"
                }
            }, {
                field: 'level',
                title: '用户等级',
                templet: function (res) {
                    return res.level == 1 ? "用户" : "管理员"
                }
            }, {
                fixed: 'right',
                title: '操作',
                toolbar: '#barDemo',
                width: 150
            }]],
            page: true,
            id: 'testReload'
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            console.log(data.userId);
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    //	obj
                    //	.del();
                    layer.close(index);
                    $.ajax({
                        url: "../../userapi/user/" + data.userId,
                        type: "delete",
                        success: function (data) {
                            layer.msg(data.msg);
                            table.reload("testReload");
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                console.log(data.date);
                $("#userid").prop("value", data.userId);
                $("#username").prop("value", data.userName);
                if (data.sex === "女") {
                    $("input[name='sex'][value='女']").prop("checked", true);
                } else {
                    $("input[name='sex'][value='男']").prop("checked", true);
                }
                $("#tel").attr("value", data.tel);
                $("#email").attr("value", data.email);
                $("#date1").attr("value", data.birthday);
                if (data.status === 0) {
                    $("[name='status'] option[value='0']").attr("selected", "true");
                }
                layui.form.render();
                laydate.render({elem: '#date1'});

                layer.open({
                    type: 1,
                    title: '修改页',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['380px', '60%'],
                    content: $("#editFormDiv"), //iframe的url
                    success: function (layero, index) {
                        layui.form.on('submit(demo1)', function (data) {
                            /* layer.alert(JSON.stringify(data.field), {
                                title : '最终的提交信息'
                            }) */
                            //表单中的数据 进行转换，得到一个json格式的字符串
                            console.log(JSON.stringify(data.field));
                            //ajax请求实现访问控制器
                            $.ajax({
                                type: "put", //提交方式
                                url: "../../userapi/user", ////地址和springmvc中控制器地址一致
                                data: data.field, //提交给控制器的数据
                                dataType: "json", //提交给控制的数据 格式
                                success: function (cm) {
                                    layer.msg(cm.msg, {time: 2000}, function () {
                                        if (cm.msg == "修改成功") {
                                            //obj.update(); ?? 理想
                                            //layer.close(index);;
                                            parent.layer.closeAll();//关闭所有,简单粗暴
                                            window.location.reload();//刷新父页面
                                        } else if (cm.msg == "修改成功，请重新登录") {
                                            window.open("login.jsp");
                                        }
                                    });
                                }
                            });
                            return false;
                        });
                    }
                });
            }
        });
        /*表格数据重载 模糊检索*/
        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#content');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1
                        //重新从第 1 页开始
                    },
                    where: { //检索的条件
                        //传递的参数 key为参数名 {} 内传递的是参数的值
                        /* key: {

                          id: demoReload.val(),
                          username :....
                        } */
                        //只有一个key : value格式
                        content: demoReload.val()
                    }
                });
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>

</html>
</body>
</html>